/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.workItem.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.workItem.dto.WorkItemAssetSearchDTO;
import com.fuyao.cloud.common.biz.dto.PageQuery;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.workItem.entity.WorkItemList;
import com.fuyao.cloud.asset.workItem.service.WorkItemListService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作事项明细
 *
 * @author lyhills
 * @date 2023-09-11 12:03:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/workItemList" )
@Api(value = "workItemList", tags = "工作事项明细管理")
public class WorkItemListController {

    private final  WorkItemListService workItemListService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param workItemList 工作事项明细
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_view')" )
    public R getWorkItemListPage(Page page, WorkItemList workItemList) {
        return R.ok(workItemListService.page(page, Wrappers.query(workItemList)));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/getWorkItemAssetPage")
    @PreAuthorize("@pms.hasPermission('workItem_workitem_view')")
    public R getWorkItemAssetPage(PageQuery<WorkItemAssetSearchDTO> pageQuery) {
        return R.ok(workItemListService.getWorkItemAssetPage(pageQuery.getPage(), SpliceQueryWrapper.spliceSearchCondition(pageQuery.getExample()), pageQuery.getExample()));
    }

    /**
     * 通过id查询工作事项明细
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(workItemListService.getById(id));
    }

    /**
     * 新增工作事项明细
     * @param workItemList 工作事项明细
     * @return R
     */
    @ApiOperation(value = "新增工作事项明细", notes = "新增工作事项明细")
    @SysLog("新增工作事项明细" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('workItem_workitem_add')" )
    public R save(@RequestBody WorkItemList workItemList) {
        boolean save = workItemListService.save(workItemList);
        return R.ok(workItemList);
    }
    /**
     * 新增工作事项明细
     * @param workItemList 工作事项明细
     * @return R
     */
    @ApiOperation(value = "新增工作事项明细", notes = "新增工作事项明细")
    @SysLog("批量新增工作事项明细" )
    @PostMapping("/batchSave")
    @PreAuthorize("@pms.hasPermission('workItem_workitem_add')" )
    public R batchAdd(@RequestBody List<WorkItemList> workItemList) {
        boolean save = workItemListService.saveBatch(workItemList);
        return R.ok(save,"保存成功！");
    }

    /**
     * 修改工作事项明细
     * @param workItemList 工作事项明细
     * @return R
     */
    @ApiOperation(value = "修改工作事项明细", notes = "修改工作事项明细")
    @SysLog("修改工作事项明细" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('workItem_workitem_edit')" )
    public R updateById(@RequestBody WorkItemList workItemList) {
        return R.ok(workItemListService.updateById(workItemList));
    }
    /**
     * 修改工作事项明细
     * @param workItemList 工作事项明细
     * @return R
     */
    @ApiOperation(value = "批量新增或修改", notes = "批量新增或修改")
    @SysLog("批量新增或修改工作事项" )
    @PutMapping("/batchSaveOrUpdate")
    @PreAuthorize("@pms.hasPermission('workItem_workitem_edit')" )
    public R batchUpdate(@RequestBody List<WorkItemList> workItemList) {
        return R.ok(this.workItemListService.saveOrUpdateBatch(workItemList));
    }

    /**
     * 通过id删除工作事项明细
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除工作事项明细", notes = "通过id删除工作事项明细")
    @SysLog("通过id删除工作事项明细" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(workItemListService.removeById(id));
    }

    /**
     * 创建方法
     * @return
     */
    @PreAuthorize("@pms.hasPermission('workItem_workitem_add')" )
    @GetMapping(value = "/create")
    public R create() {
        return R.ok(workItemListService.create());
    }

    /**
     * 导出excel 表格
     * @param workItemList 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('workItem_workitem_export')" )
    public List<WorkItemList> export(WorkItemList workItemList) {
        return workItemListService.list(Wrappers.query(workItemList));
    }

    /**
    * 批量删除方法
    * @param ids
    * @return R
    */
    @ApiOperation(value = "批量删除", notes = "批量删除通知公告")
    @SysLog("批量删除工作事项明细" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return R.ok(workItemListService.removeByIds(ids));
    }
}
